
//
//  sliderView.swift
//  CMDB
//
//  Created by 曹桂祥 on 2017/5/12.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

typealias CMDBSliderShowBlock = () -> ()

typealias CMDBSliderPushBlock = (UIViewController) -> ()

private let ratio : CGFloat = 0.86

class SliderView: CMDBBaseView {
    
    weak var superVc : CMDBBaseViewController?
    
    weak var superNav : CustomNavigationControllerViewController?
    
    var headerIcon : UIImageView!
    
    var userNameLael : UILabel!
    
    var concernButton : CMDBSocialButton!
    
    var fansButton : CMDBSocialButton!
    
    var friendsButton : CMDBSocialButton!
    
    var phantanView : UIView!
    
    var topView : UIImageView!
    
    var contentView : UIView!
    
    var userInfo : CMDBUserInfoBean?
    
    var leftMenuArr : [String]?
    
    var settingButton : UIButton?
    
    var feedBackButton : UIButton?
    
    var bottomView : UIView!
    
    var shadowView : UIView!
    
    var showSliderBlock :CMDBSliderShowBlock?
    
    var pushSliderBlock : CMDBSliderPushBlock?
    
    lazy var slideGerView : UIView = {
        
        let slideGerView = UIView.getInstance()
        
        self.addSubview(slideGerView)
        
        return slideGerView
    }()
    
    override func buildSubViews() {
        super.buildSubViews()
        
        let leftMenuArr : [String] = ["我的作品","我的资料","我的档期","我的动态","我的日记","我的相册","我的收藏"]
        
        self.leftMenuArr = leftMenuArr
        
        setupShadowView()
        
        setupHeaderView()
        
        setupConentView()
        
        setupBottom()
        
    }
}


extension SliderView{
    
    func setupShadowView(){
        
        let complView             = UIView(frame: CGRect(x: 0, y: 0, width: CMDB_Screen_Width * 2, height: CMDB_Screen_Height))
        
        shadowView                = UIView.getInstance()
        
        shadowView                = complView
        
        complView.backgroundColor = word54Color
        
        complView.alpha           = 0
        
        addSubview(shadowView)
        
        addSubview(complView)
        
        slideGerView.backgroundColor = UIColor.clearColor()
        
        slideGerView.snp_makeConstraints { (make) in
            make.top.equalTo(0)
            make.bottom.equalTo(0)
            make.right.equalTo(0)
            make.width.equalTo(20)
        }
        
        //pan手势
        let pan    = UIPanGestureRecognizer(target: self, action: #selector(panGesture(_:)))
        
        slideGerView.addGestureRecognizer(pan)
        
        //pan1手势
        let pan1   = UIPanGestureRecognizer(target: self, action: #selector(panGes(_:)))
        
        //tapTag手势
        let tapTag = UITapGestureRecognizer(target: self, action: #selector(didSlideBtn))
        
        complView.addGestureRecognizer(tapTag)
        
        complView.addGestureRecognizer(pan1)
    }
    
    func setupHeaderView(){
        
        let topView        = UIImageView.getInstance()
        
        topView.backgroundColor = UIColor.lightGrayColor()
        
        self.topView       = topView
        
        topView.frame      = CGRect(x: 0, y: 0, width: ratio * CMDB_Screen_Width, height: ratio * CMDB_Screen_Width * 0.5)
        
        
        
        addSubview(topView)
        
        headerIcon         = UIImageView.getInstance()
        
        headerIcon.image   = UIImage(named: "test头像")
        
        headerIcon.frame   = CGRect(x: KleftMargin, y: 53, width: 75, height: 75)
        
        headerIcon.userInteractionEnabled = true
        
        topView.addSubview(headerIcon)
        
        userNameLael       = UILabel.getInstance()
        
        userNameLael.textColor = UIColor.whiteColor()
        
        userNameLael.frame = CGRect(x: CGRectGetMaxX(self.headerIcon.frame) + 10, y: 90, width: 100, height: 16)
        
        topView.addSubview(userNameLael)
        
        phantanView        = UIView.getInstance()
        
        topView.addSubview(phantanView)
        
        phantanView.backgroundColor = word38Color
        
        phantanView.snp_makeConstraints { (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
            make.height.equalTo(45)
        }
        
        concernButton      = CMDBSocialButton()
        
        concernButton.nameLael?.text = "关注"
        
        concernButton.numLabel?.text   = "22"
        
        concernButton.frame = CGRect(x: 0, y: 0, width: ratio * CMDB_Screen_Width / 3, height: 41)
        
        phantanView.addSubview(concernButton)
        
        fansButton          = CMDBSocialButton()
        
        fansButton.numLabel?.text = "123"
        
        fansButton.nameLael?.text = "粉丝"
        
        fansButton.frame          = CGRect(x: ratio * CMDB_Screen_Width / 3, y: 0, width: ratio * CMDB_Screen_Width / 3, height: 41)
        
        phantanView.addSubview(fansButton)
        
        friendsButton                = CMDBSocialButton()
        
        friendsButton.numLabel?.text = "123"
        
        friendsButton.nameLael?.text = "好友"
        
        friendsButton.frame          = CGRect(x: 2 * ratio * CMDB_Screen_Width / 3, y: 0, width: ratio * CMDB_Screen_Width / 3, height: 41)
        
        phantanView.addSubview(friendsButton)
        
    }
    
    func setupConentView(){
        
        let contentView = UIView.getInstance()
        
        contentView.frame = CGRect(x: 0, y: CGRectGetMaxY(topView.frame), width:ratio * CMDB_Screen_Width , height: CMDB_Screen_Height - topView.frame.height - KTabBarHeight)
        
        contentView.backgroundColor = UIColor.whiteColor()
        
        addSubview(contentView)
        
        for (index,title) in leftMenuArr!.enumerate()   {
            
            let leftButton = CMDBLeftMenuButton()
            
            leftButton.setImage(UIImage(named: title), forState: .Normal)
            
            leftButton.tag       = index
            
            leftButton.titleLabel?.font = CMDB_13_FONT
            
            leftButton.setTitle(title, forState: .Normal)
            
            leftButton.frame     = CGRect(x: 0, y: KTabBarHeight * CGFloat(index), width: ratio * CMDB_Screen_Width, height: KTabBarHeight)
            
            leftButton.addTarget(self, action: #selector(leftButtonClicked(_:)), forControlEvents: .TouchUpInside)
            
            leftButton.contentHorizontalAlignment = .Left
            
            leftButton.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0)
            
            leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0)
            
            leftButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
            
            contentView.addSubview(leftButton)
        }
    }
    
    func setupBottom(){
        
        let bottomView = UIView()
        
        bottomView.backgroundColor = UIColor.whiteColor()
        
        bottomView.frame = CGRect(x: 0 , y: CMDB_Screen_Height - KTabBarHeight, width: ratio * CMDB_Screen_Width, height: KTabBarHeight)
        
        self.bottomView = bottomView
        
        addSubview(bottomView)
        
        let settingButton = UIButton()
        
        settingButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
        
        settingButton.titleLabel?.font = CMDB_13_FONT
        
        settingButton.setTitle("设置", forState: .Normal)
        
        settingButton.frame = CGRect(x: 0, y: 0, width: ratio * CMDB_Screen_Width * 0.5, height: KTabBarHeight)
        
        settingButton.layer.borderColor = word12Color.CGColor
        
        settingButton.layer.borderWidth = 0.5
        
        settingButton.addTarget(self, action: #selector(settingItemClick), forControlEvents: .TouchUpInside)
        
        bottomView.addSubview(settingButton)
        
        let feedBackButtoon = UIButton()
        
        feedBackButtoon.setTitle("反馈", forState: .Normal)
        
        feedBackButtoon.layer.borderColor = word12Color.CGColor
        
        feedBackButtoon.layer.borderWidth = 0.5
        
        feedBackButtoon.titleLabel?.font = CMDB_13_FONT
        
        feedBackButtoon.setTitleColor(UIColor.blackColor(), forState: .Normal)
        
        feedBackButtoon.frame = CGRect(x: ratio * CMDB_Screen_Width * 0.5, y: 0, width: ratio * CMDB_Screen_Width * 0.5, height: KTabBarHeight)
        
        bottomView.addSubview(feedBackButtoon)
        
        topView.userInteractionEnabled = true
    }
    
}


//MARK:- 手势相关
extension SliderView{
    
    //pan手势
    @objc func panGesture(recognizer : UIPanGestureRecognizer){
        
        superNav?.view.width = CMDB_Screen_Width * 2
        
        let trans   = recognizer.translationInView(recognizer.view)
        
        let changeX = self.superNav!.view.transform.tx + trans.x
        
        if changeX >= 0 && changeX <= (CMDB_Screen_Width)  {
            self.superNav?.view.transform = CGAffineTransformTranslate(self.superNav!.view.transform, trans.x, 0)
            
            self.shadowView.alpha         = changeX * 0.0015
        }
        
        if recognizer.state == .Ended && self.superNav!.view.transform.tx < (CMDB_Screen_Width * 0.5) {
            self.didSlideBtn()
        }else if recognizer.state == .Ended && self.superNav!.view.transform.tx > (CMDB_Screen_Width * 0.5){
            if  (self.showSliderBlock != nil){
                self.showSliderBlock!()
                
            }
        }else{
            
        }
        
        recognizer.setTranslation(CGPointZero, inView: recognizer.view)
    }
    
    //pan1手势
    @objc func panGes(recognizer : UIPanGestureRecognizer){
        
        superNav?.view.width = CMDB_Screen_Width * 2
        
        let trans   = recognizer.translationInView(recognizer.view)
        
        let changeX = self.superNav!.view.transform.tx + trans.x
        
        if changeX >= 0 && changeX <= (CMDB_Screen_Width - 9)  {
            self.superNav?.view.transform = CGAffineTransformTranslate(self.superNav!.view.transform, trans.x, 0)
            self.shadowView.alpha = changeX * 0.0015
        }
        
        if recognizer.state == .Ended && self.superNav!.view.transform.tx < (CMDB_Screen_Width * 0.5 + 100) {
            self.didSlideBtn()
        }else if recognizer.state == .Ended && self.superNav!.view.transform.tx > (CMDB_Screen_Width * 0.5 + 100){
            if  (self.showSliderBlock != nil){
                self.showSliderBlock!()
                
                printLog("pan1 ------ panGes=======showSliderBlock")
            }
        }else{
            
        }
        
        recognizer.setTranslation(CGPointZero, inView: recognizer.view)
    }
}

//MARK:- action methods
extension SliderView{
    
    @objc func leftButtonClicked(sender : UIButton){
        
        let index = sender.tag
        
        let title = sender.titleLabel?.text
        
        //        let userCenterVc = CMDBProductionEditViewController()
        let userCenterVc = CMDBUserCenterViewController()
        
        userCenterVc.title = title
        
        userCenterVc.hidesBottomBarWhenPushed = true;
        
        self.superNav!.pushViewController(userCenterVc, animated: true)
        
    }
    
    //tapTag
    @objc func didSlideBtn(){
        
        UIView.animateWithDuration(0.2, animations: {
            self.superNav!.view.transform = CGAffineTransformMakeTranslation(0, 0)
            self.shadowView.alpha         = 0.0
        }) { (_) in
            self.superNav!.view.width = CMDB_Screen_Width + 15
            
        }
        
        
    }
    @objc func settingItemClick() {
        
        
        let setting = CMDBSettingViewController()
        
        setting.title = "设置"
        
        superNav?.pushViewController(setting, animated: true)
        
    }
}

extension SliderView {
    
    func setupUserData(userInfo : CMDBUserInfoBean){
        
        self.userInfo = userInfo
        
        self.headerIcon.setHeader(userInfo.icon)
        
        self.topView.loadPicImage(userInfo.bgUrl)
        
        self.userNameLael.text = userInfo.name
        
        self.concernButton.numLabel.text = userInfo.followCount
        
        self.friendsButton.numLabel.text = userInfo.friendCount
        
        self.fansButton.numLabel.text    = userInfo.fansCount
        
    }
    
    
}
